package com.teufandsub.socialproximity.logic;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.teufandsub.socialproximity.services.HttpService;

public class Message {
	
	private String author;
	private String dest;
	private String content;
	
	public static final String AUTHOR_NAME  = "author";
	public static final String DEST_NAME    = "dest";
	public static final String CONTENT_NAME = "content";
	
	public static final int CODE_SENDED           = 0;
	public static final int CODE_SESSION_INVALID  = 1;
	public static final int CODE_DEST_UNAVAILABLE = 2;
	public static final int CODE_CANT_SEND        = 3;
	public static final int CODE_BLACKLISTED      = 4;
	
	public Message(String author, String dest, String content) {
		super();
		this.dest = dest;
		this.author = author;
		this.content = content;
	}
	
	public String getAuthor() {
		return author;
	}
	
	public void setAuthor(String author) {
		this.author = author;
	}
	
	public String getContent() {
		return content;
	}
	
	
	public String getDest() {
		return dest;
	}

	public void setDest(String dest) {
		this.dest = dest;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public String toString() {
		return "Message : " + this.author + " - " + this.content;
	}
	
	public JSONObject send(Context context) {
		HttpService httpService = new HttpService(context);
		JSONObject ret;
		try {
			ret = httpService.sendMessage(this.toJSONObject());
		} catch (JSONException e) {
			e.printStackTrace();
			ret=null;
		}
		return ret;
	}
	
	private JSONObject toJSONObject() throws JSONException {
		JSONObject jSonObject = new JSONObject();
		jSonObject.put(AUTHOR_NAME, this.author);
		jSonObject.put(DEST_NAME, this.dest);
		jSonObject.put(CONTENT_NAME, this.content);
		
		return jSonObject;
	}
}
